/**上午10:29:57
 * @author zhangyh2
 * OrnamentalFlowerFragment.java
 * TODO
 */
package cn.com.darly.videoandroidpm.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.darly.common.DLog;
import com.darly.common.ToastApp;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseFragment;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.StaticListener;
import cn.com.darly.videoandroidpm.ui.adapter.FragmentFriendAdapter.OnRecycItemClickListener;
import cn.com.darly.videoandroidpm.ui.conversation.chat.ConversationActivity;
import cn.com.darly.videoandroidpm.ui.registerfriend.AddFriendActivity;
import cn.com.darly.videoandroidpm.ui.adapter.FragmentFriendAdapter;
import cn.com.darly.videoandroidpm.ui.biz.FriendFragmentListener.FriendFragmentInterface;
import cn.com.darly.videoandroidpm.ui.presenter.FriendFragmentPresenter;
import cn.com.darly.videoandroidpm.widget.indexlist.IndexHideBar;
import cn.com.darly.videoandroidpm.widget.indexlist.IndexSliderBar;

/**
 * 用户通讯录，这里获取的是已经注册的用户，并且添加好友。
 * Copyright (c) 2017 Organization D.L. zhangyuhui All rights reserved.
 * @author  Darly/张宇辉/2018/5/7 11:02
 * @version  1.0/cn.com.darly.videoandroidpm.ui.fragment
 */
public class OrnamentalFlowerFragment extends BaseFragment implements IndexSliderBar.OnChooseLetterChangedListener,FriendFragmentInterface ,OnClickListener,StaticListener.OnRefreshFriendListener,OnRecycItemClickListener{
	/**
	 * 有好友，展示列表
	 */
	private FrameLayout id_fragment_friend_hasfriend;
	/**
	 * 没有好友，展示空白页
	 */
	private LinearLayout id_fragment_friend_nofriend;
	/**
	 * 用户列表，展示所用的ListView
	 */
	private RecyclerView id_frgment_friend_list;
	/**
	 * 右边字母关联列表
	 */
	private IndexHideBar id_frgment_friend_slider;
	/**
	 * ListView列表适配器
	 */
	private FragmentFriendAdapter adapter;
	/**
	 * RecycleView使用的方向管理器
	 */
	private LinearLayoutManager manager;
	/**
	 * 关联请求解析类
	 */
	private FriendFragmentPresenter presenter;

	Map<String, String> params = new LinkedHashMap<String, String>();

	@Override
	protected int root() {
		return R.layout.fragment_main_ornamental;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseFragment#initView(android.os.Bundle)
	 */
	@Override
	protected void initView(Bundle savedInstanceState) {
		id_fragment_friend_hasfriend = rootView.findViewById(R.id.id_fragment_friend_hasfriend);
		id_fragment_friend_nofriend = rootView.findViewById(R.id.id_fragment_friend_nofriend);

		id_frgment_friend_slider = rootView.findViewById(R.id.id_frgment_friend_slider);
		id_frgment_friend_list = rootView.findViewById(R.id.id_frgment_friend_list);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseFragment#loadData()
	 */
	@Override
	protected void loadData() {
		manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
		id_frgment_friend_list.setLayoutManager(manager);
		adapter= new FragmentFriendAdapter(getActivity());
		id_frgment_friend_list.setAdapter(adapter);

		presenter = new FriendFragmentPresenter(this);
		presenter.friend();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseFragment#initListener()
	 */
	@Override
	protected void initListener() {
		id_frgment_friend_slider.setOnChooseLetterChangedListener(this);
		id_fragment_friend_nofriend.setOnClickListener(this);
		adapter.setOnRecycItemClickListener(this);
		StaticListener.setOnRefreshFriendListener(this);
	}

	@Override
	public void onChooseLetter(String s) {
		int i = adapter.getFirstPositionByChar(s.charAt(0));
		if (i == -1) {
			return;
		}
		manager.scrollToPositionWithOffset(i, 0);
	}

	@Override
	public void onNoChooseLetter() {

	}

	@Override
	public void onSuccess(List<LoginInfo> info) {
		if (info!=null&&info.size()>0){
			id_fragment_friend_hasfriend.setVisibility(View.VISIBLE);
			id_fragment_friend_nofriend.setVisibility(View.GONE);
			adapter.setData(info);
		}else {
			id_fragment_friend_hasfriend.setVisibility(View.GONE);
			id_fragment_friend_nofriend.setVisibility(View.VISIBLE);
			id_fragment_friend_nofriend.setClickable(true);
		}
	}

	@Override
	public void onFailed(String info) {
		if (adapter.getItemCount()==0) {
			id_fragment_friend_hasfriend.setVisibility(View.GONE);
			id_fragment_friend_nofriend.setVisibility(View.VISIBLE);
			id_fragment_friend_nofriend.setClickable(false);
		}
		ToastApp.showToast(info);
	}

	@Override
	public void setDisableClick() {

	}

	@Override
	public void setEnableClick() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.id_fragment_friend_nofriend:
				startActivity(new Intent(getActivity(), AddFriendActivity.class));
				break;
		}
	}
	@Override
	public void refreshFriend() {
		DLog.i(getClass().getSimpleName(),"refreshFriend----------->Start");
		presenter.friend();
	}

	@Override
	public void OnRecycItemClick(RecyclerView parent, View view, int position, LoginInfo data) {
		//点击好友信息
		if (data!=null){
			Intent intent = new Intent(getActivity(), ConversationActivity.class);
			intent.putExtra("LoginInfo",data);
			getActivity().startActivity(intent);
		}
	}
}
